<template>
    <div class="hooks-demo">
        <h1>Hooks 工具库演示</h1>

        <!-- 主题背景色 Hook -->
        <my-el-card header="useTheme - 主题背景色管理">
            <div class="demo-section">
                <div class="theme-preview" :style="{ backgroundColor: theme.currentTheme }">
                    <p>当前背景色: {{ theme.currentTheme }}</p>
                    <p>RGB值: {{ theme.themeInfo.rgb }}</p>
                    <p>类型: {{ theme.isDark ? '深色' : '浅色' }}</p>
                    <p>对比色: {{ theme.themeInfo.contrastColor }}</p>
                </div>

                <div class="button-group">
                    <my-el-button @click="theme.setPresetTheme('light')">浅色主题</my-el-button>
                    <my-el-button @click="theme.setPresetTheme('dark')">深色主题</my-el-button>
                    <my-el-button @click="theme.setPresetTheme('blue')">蓝色主题</my-el-button>
                    <my-el-button @click="theme.setPresetTheme('green')">绿色主题</my-el-button>
                    <my-el-button @click="theme.setPresetTheme('pink')">粉色主题</my-el-button>
                </div>

                <div class="button-group">
                    <my-el-button @click="theme.setRandomTheme">随机主题</my-el-button>
                    <my-el-button @click="theme.resetTheme">重置主题</my-el-button>
                    <my-el-button @click="theme.undoTheme" :disabled="theme.themeHistory.length === 0">撤销</my-el-button>
                    <my-el-button @click="theme.clearHistory" :disabled="theme.themeHistory.length === 0">清空历史</my-el-button>
                </div>

                <div class="custom-color">
                    <p>自定义颜色:</p>
                    <div class="color-inputs">
                        <input 
                            type="color" 
                            :value="theme.currentTheme" 
                            @input="e => theme.setTheme(e.target.value)"
                            class="color-picker"
                        />
                        <input 
                            type="text" 
                            :value="theme.currentTheme" 
                            @input="e => theme.setTheme(e.target.value)"
                            placeholder="#ffffff"
                            class="color-text"
                        />
                    </div>
                </div>

                <div v-if="theme.themeHistory.length > 0" class="theme-history">
                    <p>主题历史记录:</p>
                    <div class="history-colors">
                        <div 
                            v-for="(color, index) in theme.themeHistory" 
                            :key="index"
                            class="history-color"
                            :style="{ backgroundColor: color }"
                            @click="theme.setTheme(color)"
                            :title="color"
                        ></div>
                    </div>
                </div>

                <!-- 全局主题效果演示 -->
                <div class="global-theme-demo">
                    <h4>全局主题效果演示</h4>
                    <div class="demo-components">
                        <div class="demo-card">
                            <h5>卡片组件</h5>
                            <p>这个卡片的背景色会随着主题变化</p>
                        </div>
                        <div class="demo-table">
                            <table>
                                <thead>
                                    <tr>
                                        <th>列1</th>
                                        <th>列2</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        <td>数据1</td>
                                        <td>数据2</td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                        <div class="demo-form">
                            <input type="text" placeholder="输入框背景色会变化" />
                            <select>
                                <option>下拉选择器</option>
                            </select>
                        </div>
                    </div>
                </div>
            </div>
        </my-el-card>

        <!-- 计数器 Hook -->
        <my-el-card header="useCounter - 计数器管理">
            <div class="demo-section">
                <p>当前值: {{ counter.count }}</p>
                <p>状态: {{ counter.isMin ? '已到最小值' : counter.isMax ? '已到最大值' : '正常' }}</p>
                <div class="button-group">
                    <my-el-button @click="counter.decrement" :disabled="counter.isMin">-</my-el-button>
                    <my-el-button @click="counter.increment" :disabled="counter.isMax">+</my-el-button>
                    <my-el-button @click="counter.reset">重置</my-el-button>
                    <my-el-button @click="counter.setValue(5)">设为5</my-el-button>
                </div>
            </div>
        </my-el-card>

        <!-- 切换状态 Hook -->
        <my-el-card header="useToggle - 状态切换">
            <div class="demo-section">
                <p>当前状态: {{ toggle.state }}</p>
                <div class="button-group">
                    <my-el-button @click="toggle.toggle">切换</my-el-button>
                    <my-el-button @click="toggle.setTrue">设为True</my-el-button>
                    <my-el-button @click="toggle.setFalse">设为False</my-el-button>
                </div>

                <p>主题切换: {{ theme.state }}</p>
                <div class="button-group">
                    <my-el-button @click="theme.toggle">切换主题</my-el-button>
                </div>
            </div>
        </my-el-card>

        <!-- 本地存储 Hook -->
        <my-el-card header="useLocalStorage - 本地存储">
            <div class="demo-section">
                <p>存储的用户名: {{ storage.value.name || '未设置' }}</p>
                <div class="button-group">
                    <my-el-button @click="storage.setValue({ name: '张三', age: 25 })">设置用户</my-el-button>
                    <my-el-button @click="storage.remove">清除存储</my-el-button>
                </div>

                <p>临时数据: {{ tempData.value || '无' }}</p>
                <div class="button-group">
                    <my-el-button @click="tempData.setValue('临时保存的数据')">保存临时数据</my-el-button>
                    <my-el-button @click="tempData.clear">清空临时数据</my-el-button>
                </div>
            </div>
        </my-el-card>


        <!-- 异步操作 Hook -->
        <my-el-card header="useAsync - 异步操作">
            <div class="demo-section">
                <div v-if="asyncDemo.loading" class="loading">
                    <p>加载中...</p>
                </div>
                <div v-else-if="asyncDemo.error" class="error">
                    <p>错误: {{ asyncDemo.error.message }}</p>
                    <my-el-button @click="asyncDemo.execute">重试</my-el-button>
                </div>
                <div v-else-if="asyncDemo.data" class="success">
                    <p>成功获取数据: {{ asyncDemo.data }}</p>
                    <my-el-button @click="asyncDemo.execute">刷新</my-el-button>
                </div>
                <div v-else>
                    <p>点击按钮开始异步操作</p>
                    <my-el-button @click="asyncDemo.execute">开始请求</my-el-button>
                </div>

                <div class="button-group">
                    <my-el-button @click="asyncDemo.reset">重置状态</my-el-button>
                </div>
            </div>
        </my-el-card>
    </div>
</template>

<script setup>
import { useCounter, useToggle, useLocalStorage, useAsync, useTheme } from '@/hooks'

// 主题背景色 Hook
const theme = useTheme('#000000', {
    storage: 'localStorage',
    storageKey: 'demo-theme-v2', // 改个key
    autoApply: true,
    cssVarPrefix: '--demo'
})

// 计数器 Hook - 范围 0-10，步长 1
const counter = useCounter(0, 0, 10, 1)

// 切换状态 Hook - 布尔值切换
const toggle = useToggle(false)

// 主题切换 Hook - 多值切换
const themeToggle = useToggle('light', ['light', 'dark', 'auto'])

// 本地存储 Hook - 用户信息持久化
const storage = useLocalStorage('demo-user', { name: '', age: 0 })

// 临时存储 Hook - 使用 sessionStorage
const tempData = useLocalStorage('demo-temp', null, { storage: 'sessionStorage' })

// 异步操作 Hook - 模拟 API 请求
const asyncDemo = useAsync(
    async () => {
        // 模拟网络延迟
        await new Promise(resolve => setTimeout(resolve, 1000))

        // 模拟随机成功/失败
        if (Math.random() > 0.3) {
            return `数据 ${Date.now()}`
        } else {
            throw new Error('网络请求失败')
        }
    },
    {
        onSuccess: (data) => console.log('异步操作成功:', data),
        onError: (error) => console.error('异步操作失败:', error)
    }
)
</script>

<style scoped lang="scss">
.hooks-demo {
    padding: 20px;
}

.demo-section {
    margin-bottom: 16px;

    p {
        margin: 8px 0;
        color: #606266;
    }
}

.button-group {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    margin: 12px 0;
}

.loading {
    color: #409eff;
    font-weight: 500;
}

.error {
    color: #f56c6c;
    font-weight: 500;
}

.success {
    color: #67c23a;
    font-weight: 500;
}

// 主题相关样式
.theme-preview {
    padding: 20px;
    border-radius: 8px;
    margin: 16px 0;
    text-align: center;
    min-height: 120px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    border: 2px solid #e4e7ed;
    
    p {
        margin: 4px 0;
        font-weight: 500;
        text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
    }
}

.custom-color {
    margin: 16px 0;
    
    .color-inputs {
        display: flex;
        gap: 8px;
        align-items: center;
        margin-top: 8px;
    }
    
    .color-picker {
        width: 40px;
        height: 40px;
        border: none;
        border-radius: 4px;
        cursor: pointer;
    }
    
    .color-text {
        flex: 1;
        padding: 8px 12px;
        border: 1px solid #dcdfe6;
        border-radius: 4px;
        font-family: monospace;
        
        &:focus {
            outline: none;
            border-color: #409eff;
        }
    }
}

.theme-history {
    margin: 16px 0;
    
    .history-colors {
        display: flex;
        gap: 8px;
        flex-wrap: wrap;
        margin-top: 8px;
    }
    
    .history-color {
        width: 32px;
        height: 32px;
        border-radius: 4px;
        cursor: pointer;
        border: 2px solid #e4e7ed;
        transition: transform 0.2s;
        
        &:hover {
            transform: scale(1.1);
            border-color: #409eff;
        }
    }
}

// 全局主题演示样式
.global-theme-demo {
    margin: 20px 0;
    padding: 20px;
    border: 1px solid #e4e7ed;
    border-radius: 8px;
    
    h4 {
        margin: 0 0 16px 0;
        color: #606266;
        font-size: 16px;
    }
    
    .demo-components {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
        gap: 16px;
    }
    
    .demo-card {
        padding: 16px;
        border: 1px solid #e4e7ed;
        border-radius: 6px;
        background-color: var(--demo-bg-color, #ffffff);
        transition: background-color 0.3s ease;
        
        h5 {
            margin: 0 0 8px 0;
            color: #303133;
        }
        
        p {
            margin: 0;
            color: #606266;
        }
    }
    
    .demo-table {
        table {
            width: 100%;
            border-collapse: collapse;
            background-color: var(--demo-bg-color, #ffffff);
            transition: background-color 0.3s ease;
            
            th, td {
                padding: 8px 12px;
                text-align: left;
                border: 1px solid #e4e7ed;
                background-color: var(--demo-bg-color, #ffffff);
                transition: background-color 0.3s ease;
            }
            
            th {
                background-color: rgba(var(--demo-bg-color-rgb, 255, 255, 255), 0.1);
                font-weight: 600;
                color: #303133;
            }
        }
    }
    
    .demo-form {
        display: flex;
        flex-direction: column;
        gap: 12px;
        
        input, select {
            padding: 8px 12px;
            border: 1px solid #e4e7ed;
            border-radius: 4px;
            background-color: rgba(var(--demo-bg-color-rgb, 255, 255, 255), 0.05);
            transition: all 0.3s ease;
            
            &:focus {
                outline: none;
                border-color: #409eff;
            }
        }
    }
}
</style> 